package com.gl.wxstore.store.controller;

import java.util.List;
import java.util.UUID;

import com.gl.wxstore.common.BaseController;
import com.gl.wxstore.common.model.StoreCart;
import com.gl.wxstore.common.model.StoreOrder;
import com.gl.wxstore.common.model.StoreReceiveAddress;
import com.gl.wxstore.common.model.SysUser;
import com.gl.wxstore.store.service.OrderService;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Record;

/**
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 *
 * @author Liberty
 * @since Jan 9, 2017
 */
public class OrderController extends BaseController {
	private OrderService orderService = Duang.duang(OrderService.class);

	public void addOrder() {
		StoreOrder order = getModel(StoreOrder.class);
		StoreCart.dao.changeCartState(order.getStoreOrderProductId());
		order.setId(UUID.randomUUID().toString());
		order.setState(1);
		order.setPayState(1);
		order.save();
		redirect("/product/listProduct.html");
	}

	public void toAccount() {
		SysUser user = (SysUser) getSessionAttr("user");
		List<Record> orderProductList = orderService.listOrderProduct(user.getId());
		StoreReceiveAddress receiveAddress = StoreReceiveAddress.dao.getFirst(user.getId());

		setAttr("orderProductList", orderProductList);
		setAttr("receiveAddress", receiveAddress);

		render("/views/store/orderlist.jsp");
	}

}
